package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpSafeTableExcelReturnBean;
import com.joysuch.wwyt.bp.bean.investigate.BpSafeTableBean;
import com.joysuch.wwyt.bp.bean.investigate.BpSafeTableQueryBean;
import com.joysuch.wwyt.bp.service.BpSafeTableService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.dataimport.service.DataImportService;
import com.joysuch.wwyt.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.InputStream;

/**
 * @Author ZhangDong
 * @Date 2020/12/2 13:26
 */
@RestController
@RequestMapping("bp/table")
@Api(tags = "bp/table 安全排查表")
@Slf4j
public class BpSafeTableController {

    @Autowired
    private BpSafeTableService tableService;
    @Autowired
    private DataImportService dataImportService;

    @PostMapping("save")
    @ApiOperation("安全排查表--新增/修改")
    @RequiresPermissions("bp-table:save")
    public ResultBean save(@Valid @RequestBody BpSafeTableBean bean) {
        return tableService.save(bean);
    }

    @GetMapping("delete")
    @ApiOperation("安全排查表--删除")
    @RequiresPermissions("bp-table:delete")
    public ResultBean delete(Long id) {
        return tableService.delete(id);
    }

    @GetMapping("page")
    @ApiOperation("分页查询")
    public ResultBean page(BpSafeTableQueryBean bean) {
        return tableService.page(bean);
    }

    @GetMapping("get")
    @ApiOperation("单体查询排查表")
    public ResultBean get(Long id) {
        return tableService.get(id);
    }

    @GetMapping("download/template")
    @ApiOperation(value = "下载导入模板")
    // @RequiresPermissions("bp-table:download:template")
    public void download(HttpServletResponse response, HttpServletRequest request) {
        String fileName = "安全排查表导入模板.xlsx";
        ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + fileName);
        try (ServletOutputStream outputStream = response.getOutputStream();
             InputStream inputStream = classPathResource.getInputStream()) {
            fileName = FileUtil.getEncodeFileName(request, fileName);
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            byte[] data = new byte[1024 * 10];
            int len = -1;
            while ((len = inputStream.read(data)) != -1) {
                outputStream.write(data, 0, len);
            }
        } catch (Exception e) {
            log.error("安全排查表导入模板.xlsx 下载失败", e);
        }
    }

    @PostMapping("import/data")
    @ApiOperation("导入数据(解析数据返回给前端)")
    @RequiresPermissions("bp-table:import:data")
    public ResultBean importData(MultipartFile file) {
        BpSafeTableExcelReturnBean eB = dataImportService.importSafeTableData(file);
        return ResultBean.success(eB);
    }
}